extends "res://scene/object/plane.gd"

var level = 0 # 等级
var exp = 0 # 经验值
var onfire = false

 


func _ready():
	super._ready()
	global.player = self
	hpBar = $"../UI/ProgressBar"
	emit_signal("status_changed")
	
	
func _process(delta):
	var xdir = Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	velocity.x = xdir * agi
	
	var ydir = Input.get_action_strength("ui_down") - Input.get_action_strength("ui_up")
	velocity.y = ydir * agi
	
	move_and_slide()

func update_status():
	print("update_status_info")
	var info = "Level: {level}  Exp: {exp}".format({"level": level, "exp":exp})
	$"../UI/statusLabel".text = info
	
	hpBar.max_value = maxhp
	hpBar.value = hp
	$"../UI/ProgressBar/hpLabel".text="{0}/{1}".format([hp, maxhp])

# 按件发射子弹
func _input(event):
	if event.is_action_pressed("shoot"):
		onfire = true
		fire(false)
		shoot_timer.start()
	elif event.is_action_released("shoot"):
		onfire = false
		shoot_timer.stop()
		
func _on_shoot_timer_timeout():
	if onfire:
		fire(false)
			
func add_exp(v):
	exp+=v
	emit_signal("status_changed")
	if exp/50 > level:
		level_up()
		

func level_up():
	
	print("exp="+str(exp))
	print("level_up")
	
	# 升级动画 音效
	$AnimationPlayer.play("level_up")
	global.play_me("level_up")
	
	level+=1
	maxhp+=2
	hp+=2
	if freq>0.2:
		freq-=0.1
		shoot_timer.wait_time = freq
	
	emit_signal("status_changed")
	
			
func get_item(id):
	global.play_me("get_item")
	# print("get_item="+str(id))		
	match id:
		0: #补血
			change_hp(5)
		1: #获得经验
			add_exp(10)
		2: #获得盾
			pass
		5: 	# 高能炮
			pass
		6: # 散弹
			for node in get_node("guns").get_children():
				if node.is_in_group("direct_shoot"):
					node.hide()	
				elif node.is_in_group("polar_shoot"):
					node.show()				
		7: #其他
			pass

func _on_animation_player_animation_finished(anim_name):
	#print("_on_animation_player_animation_finished", anim_name)
	if anim_name == "level_up":
		if level<4:
			$skin.frame=level
		else:
			$skin.frame=3
		  
